package com.hskj.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hskj.dao.CategoryDao;
import com.hskj.info.PageInfo;
import com.hskj.localUtil.LocalPageUtil;
import com.hskj.model.Category;
import com.hskj.model.CategoryQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryService {
    @Autowired
    private CategoryDao categoryDao;

    public List<Category> findAll(){
        return categoryDao.findAll();
    }

    public List<Category> select(CategoryQuery query){
        if(query.getPageNum()!=null && query.getPageSize()!= null){
            PageHelper.startPage(query.getPageNum(),query.getPageSize());
        }

        List<Category> select = categoryDao.select(query);
        if(select instanceof Page){
            Page page=(Page) select;
            long total=page.getTotal();
            System.out.println(total);
            LocalPageUtil.set(PageInfo.builder().Total(total).build());
        }

        return select;
    }

    public Integer insert(Category category){
        return   categoryDao.insert(category);
    }
    public Integer update(Category category){
        return   categoryDao.update(category);
    }
    public Integer delete(Integer id){
        return   categoryDao.updateStatus(id,0);
    }

}
